<?php
class user extends CI_Controller
{
    protected $_table = "mock_user";

    public function __construct(){
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('user_model');
		$this->load->helper('url');
    }


    public function index(){
        $data['title'] = "List Users";
        $data['path'] = "users/list_user";
        // Pagination
        $config['base_url'] = base_url().'admincp/user/index'; //url page cần PT
        $config['total_rows'] = $this->user_model->totalUsers(); //Total record
        $config['uri_segment'] = 4;
        //css pag
        $config['full_tag_open'] = '<ul class="tsc_pagination tsc_paginationA tsc_paginationA01">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_link'] = '&lt;';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="current"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        $config['first_link'] = '&lt;&lt;';
        $config['last_link'] = '&gt;&gt;';
        //end
        $config['per_page'] = 3;  // Số record hiển thị trên 1 trang
        $this->pagination->initialize($config);
        $start = $this->uri->segment(4);
        // End pagination
        $data['listusers'] = $this->user_model->listUsers($config['per_page'],$start);
        //$this->debug($data['listusers']);
        $this->load->view("index",$data);
    }


    public function updateUser(){
        $id = $this->uri->segment(4);
        $data['title'] = "Edit Users";
        $data['path'] = "users/update";
        $data['errors'] = "";
        $data['info'] = $this->user_model->getdata($id);
        if($this->input->post('submit') != NULL){
            $this->form_validation->set_rules('name','Username','required|min_length[3]|max_length[32]');
            $this->form_validation->set_rules('pass','Password','required');
            $this->form_validation->set_rules('repass','RePass','required');
            $this->form_validation->set_rules('fullname','Fullname','required');
            $this->form_validation->set_rules('email','Email','required|valid_email');
            $this->form_validation->set_rules('phone','Phone','required');
            if($this->form_validation->run()  == NULL){
                $this->load->view('index',$data);
            }else{
                $name = $this->input->post("name");
                $pass = $this->input->post("pass");
                $repass = $this->input->post("repass");
                $fullname = $this->input->post("fullname");
                $gender = $this->input->post("gender");
                $phone = $this->input->post("phone");
                $email = $this->input->post("email");
                $level = $this->input->post("level");
                $dbarray = array(
                    "user_name" => $name,
                    "user_password" => $pass,
                    "user_fullname" => $fullname,
                    "user_gender" => $gender,
                    "user_phone" => $phone,
                    "user_email" => $email,
                    "user_level" => $level
                );
                $checkUsername = $this->user_model->checkUser("user_name",$name,$id);
                $checkEmail = $this->user_model->checkUser("user_email",$email,$id);
                if($checkUsername == FALSE){
                    $data['errors'] = "Username đã tồn tại";
                    $this->load->view('index',$data);
                }elseif($checkEmail == FALSE){
                    $data['errors'] = "Email đã tồn tại";
                    $this->load->view('index',$data);
                }
                elseif($pass != $repass){
                    $data['errors'] = "Nhập lại password sai";
                    $this->load->view('index',$data);
                }else{
                $this->user_model->update($id,$dbarray);
                redirect(base_url()."index.php/admin/users");
                exit();
                }
            }
        }else{
            $this->load->view('index',$data);
        }
    }

    public function deleteUser(){
        $id = $this->uri->segment(4);
        $this->user_model->delete($id);
        redirect(base_url()."index.php/admin/users");
    }


    public function sortUser(){
        if($this->input->post['sort'] != NULL){
            $type=$this->input->post['type'];
            $order=$this->input->post['order'];
            $this->user_model->sort($type,$order);
        }

    }
}